package com.xhs.train;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class ZookeeperDemo {
    public static void main(String[] args)  throws Exception{

        final CountDownLatch countDownLatch = new CountDownLatch(1);
        String host = "127.0.0.1:2180,127.0.0.1:2181,127.0.0.1:2182";

        //创建连接
        ZooKeeper zk = new ZooKeeper(host, 30000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    countDownLatch.countDown();
                }
                System.out.println("state==="+watchedEvent.getState());
                System.out.println("eventType === " + watchedEvent.getType());
                System.out.println("path===" + watchedEvent.getPath());
            }
        });

        try {
            countDownLatch.await();
        }catch (Exception e){
            e.printStackTrace();
        }


        //注意：这里需要先手动创建 /demo_service 节点
        // 获取子节点列表
        List<String> children = zk.getChildren("/demo_service", true);
        System.out.println(children);


        // 创建节点 不限制权限
        if(zk.exists("/demo_service/dd1",true)==null){
            zk.create("/demo_service/dd1","dd1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
        }

        // 创建节点 限制权限
        List<ACL> aclList = new ArrayList<ACL>();
        ACL acl1 = new ACL();
        acl1.setId(new Id("digest",DigestAuthenticationProvider.generateDigest("xiongkw:12345")));
        acl1.setPerms(31);

        ACL acl2 = new ACL();
        acl2.setId(new Id("digest",DigestAuthenticationProvider.generateDigest("demo:12345")));
        acl2.setPerms(31);
        aclList.add(acl1);
        aclList.add(acl2);

        if(zk.exists("/demo_service/dd2",true)==null){
            zk.create("/demo_service/dd2","dd".getBytes(),aclList,CreateMode.PERSISTENT);
        }

        //查询权限信息
        System.out.println(zk.getACL("/demo_service/dd2",null));

        //给会话添加用户认证
        zk.addAuthInfo("digest","xiongkw:12345".getBytes());

        // 删除节点
        if (zk.exists("/demo_service/dd1",true) != null){
            zk.delete("/demo_service/dd1",-1);
        }

        // 修改节点数据 修改最新版本的数据
        Stat stat2 = zk.setData("/demo_service", "dd".getBytes(), -1);

        Stat stat = zk.exists("", false);
        // 修改节点数据 修改指定版本的数据
        zk.setData("/demo_service","dd2".getBytes(),stat2.getVersion());

        // 读取最新版本的数据
        byte[] data = zk.getData("/demo_service", true, null);
        System.out.println(String.valueOf(data));

        // 读取特定版本的数据
        byte[] data1 = zk.getData("/demo_service", true, stat2);
        System.out.println(String.valueOf(data));

        //判断节点是否存在
        System.out.println(zk.exists("/demo_service",true));

        zk.close();
    }
}
